package cn.glinks.liuhe.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.glinks.liuhe.R;
import cn.glinks.liuhe.adpter.TagAdapter;
import cn.glinks.liuhe.app.AppApplication;
import cn.glinks.liuhe.app.AppConstant;
import cn.glinks.liuhe.base.BaseActivity;
import cn.glinks.liuhe.model.Interest;
import cn.glinks.liuhe.utils.ActTransAnimUtils;
import cn.glinks.liuhe.utils.LogUtils;
import cn.glinks.liuhe.utils.SPUtils;
import cn.glinks.liuhe.widget.flowtaglayout.FlowTagLayout;
import cn.glinks.liuhe.widget.flowtaglayout.OnTagSelectListener;

/**
 * Des:爱好选择页面
 *
 * @author MarsBlade of Cyh
 * @created 2017-03-21 14:43
 */
public class InterestActivity extends BaseActivity {

    @BindView(R.id.tv_title)
    TextView mTitleText;

    @BindView(R.id.ftl_label)
    FlowTagLayout mFlowTagLayout;

    // 变量
    private String mTagTitle = "";//tag一级标签
    private List<Interest> mTagList;
    private TagAdapter<Interest> mTagAdapter;
    private boolean isChanged = false;

    @Override
    public int getLayoutId() {
        return R.layout.activity_interest;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        // 设置为透明状态栏
        SetTranslanteBar();
        // 获取传递过来的信息
        Bundle bundle = this.getIntent().getExtras();
        mTagTitle = bundle.getString(AppConstant.BUNDLE_INTEREST_TAG_NAME);// 获取标题
        // 加载数据
        mTagList = getData(mTagTitle);
        // 初始化标题
        mTitleText.setText(mTagTitle);
        // 初始化FlowTagLayout
        mTagAdapter = new TagAdapter<>(this, mTagList);
        mFlowTagLayout.setTagCheckedMode(FlowTagLayout.FLOW_TAG_CHECKED_MULTI);
        mFlowTagLayout.setAdapter(mTagAdapter);
        mTagAdapter.notifyDataSetChanged();
        // 设置选择监听
        mFlowTagLayout.setOnTagSelectListener(new OnTagSelectListener() {
            @Override
            public void onItemSelect(FlowTagLayout parent, int positon, List<Integer> selectedList) {
                isChanged = true;
                if (mTagList.get(positon).isChecked) {
                    mTagList.get(positon).isChecked = false;
                } else {
                    mTagList.get(positon).isChecked = true;
                }
                // 更新视图
                mTagAdapter.notifyDataSetChanged();
            }
        });
    }

    /**
     * Des:加载数据
     * @param title 爱好一级类别
     * @return
     */
    private List<Interest> getData(String title) {
        // 加载标签和选中数据
        LogUtils.logd("title="+title);
        List<Interest> list = null;
        if (title.equals(AppConstant.INTEREST_HOBBY)) {
            list = ((AppApplication)getApplicationContext()).getHobbyList();
        } else if (title.equals(AppConstant.INTEREST_SPORT)) {
            list = ((AppApplication)getApplicationContext()).getSportList();
        } else if (title.equals(AppConstant.INTEREST_TRAVEL)) {
            list = ((AppApplication)getApplicationContext()).getTravelList();
        } else if (title.equals(AppConstant.INTEREST_INTERCOURSE)) {
            list = ((AppApplication)getApplicationContext()).getIntercourseList();
        } else if (title.equals(AppConstant.INTEREST_LIVING)) {
            list = ((AppApplication)getApplicationContext()).getLivingList();
        } else if (title.equals(AppConstant.INTEREST_CHARACTER)) {
            list = ((AppApplication)getApplicationContext()).getCharacterList();
        } else {
            list = null;
        }

        LogUtils.logd("list="+list);

        return list;
    }

    /*--------------------------------------------------------------------------------
     *                          点击事件接口实现
     ---------------------------------------------------------------------------------*/
    // 确认
    @OnClick(R.id.tv_confirm)
    public void clickConfirm() {
        // 保存数据到SharedPreference文件
        SPUtils.setSharedBooleanData(this, AppConstant.IS_INTEREST_CHANGED, isChanged);
        // 返回
        ActTransAnimUtils.finishActionSlideLeftToRight(InterestActivity.this);
    }

    /**
     * 监听主页键,返回键
     *
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            // 保存数据到SharedPreference文件
            SPUtils.setSharedBooleanData(this, AppConstant.IS_INTEREST_CHANGED, isChanged);
            // 返回
            ActTransAnimUtils.finishActionSlideLeftToRight(InterestActivity.this);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
